/** @noSelfInFile **/
import {GlobalModel} from "../model1/GlobalModel";
import {HandleModel} from "../model1/HandleModel";
import {IRectAdapter, rectAdapter} from "../../adapter/adapterInterface/model2/IRectAdapter";


export class RectModel implements HandleModel {

  public handle: any;
  id: string;
  nikeName: string;

  rectAdapter: IRectAdapter;

  constructor(name: string) {
    this.nikeName = name;
    this.rectAdapter = rectAdapter.adapter;

    const handle = this.rectAdapter.createHandle(name);
    if (handle) {
      this.bindHandle(handle)
      this.id = name;
    } else {
      GlobalModel.toast(`${name} 矩形区域未找到`)
    }
  }

  getHandleId(): string {
    return this.id;
  }


  public getCenterLocModel() {
    return this.rectAdapter.getCenterLocModel(this.handle);
  }
  public getRandomLocModel() {
    return this.rectAdapter.getRandomLocModel(this.handle);
  }

  public enumDestructables(actionFunc: (this: void) => void) {
    this.rectAdapter.enumDestructables(this.handle, actionFunc)
  }

  public enumItems(actionFunc: (this: void) => void) {
    this.rectAdapter.enumItems(this.handle, actionFunc)
  }

  destroy() {
    this.rectAdapter.destroy(this.handle)
    this.handle = undefined;
  }

  bindHandle(handle: any) {
    this.rectAdapter.bindHandle(handle)
    this.handle = handle;
  }

  equal(handle: RectModel): boolean {
    return this.rectAdapter.equal(this.handle, handle.handle)
  }

  isValid(): boolean {
    return this.rectAdapter.isValid(this.handle)
  }

}
